<template>
  <div class="register-container">
    <el-card class="register-card">
      <div slot="header" class="clearfix" style="text-align: center;color: #0bc162;font-size: 16px">
        <el-tabs v-model="tabName">
          <el-tab-pane label="登录" name="login">

          </el-tab-pane>
          <el-tab-pane label="注册" name="register">

          </el-tab-pane>
        </el-tabs>
      </div>
      <el-form ref="loginForm" :rules="loginRules" :model="loginData" label-position="top" label-width="0px">
        <el-form-item prop="username" label="账号">
          <el-input v-model="loginData.username" prefix-icon="el-icon-user" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input
              v-model="loginData.password"
              prefix-icon="el-icon-lock"
              type="password"
              placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <div class="f-r a-c j-b" style="margin-top: 10px">
            <el-button
                :loading="isLoading"
                :disabled="isLoading"
                style="width: 47%"
                type="primary"
                size="small"
                @click="login"
            >
              {{ tabName === 'login' ? isLoading ? '登录中' : '登录' :isLoading ? '注册中' : '注册' }}
            </el-button>
            <el-button
                style="width: 47%"
                type="primary"
                size="small"
                @click="reset"
            >
              重置
            </el-button>
          </div>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>
<script>
export default {
  name: 'Login',
  data() {
    return {
      isLoading: false,
      loginData: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      tabName: 'login'
    }
  },
  computed: {
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(async isPassed => {
        if (isPassed) {
          this.isLoading = true
          this.$store.dispatch('user/login', { ...this.loginData }).then(res => {
            this.$router.push('/')
          }).catch((error) => {
            console.log('error', error)
            this.$message.error(error)
            this.isLoading = false
          })
        }
      })
    },
    reset() {
      this.loginData.username = ''
      this.loginData.password = ''
    }
  }
}
</script>

<style scoped lang="scss">
.register-container {
  width: 100%;
  height: 100%;
  background-image: url("~@/assets/login_bg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  .register-card {
    width: 500px;
    background: #fff;
    border-radius: 5px;
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
